package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_ClassifySecondVPAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.Discover_ClassifySecondGetTabService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifySecondBookTabEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoverClassifySecondBaseActivity extends AppCompatActivity implements Callback<Discover_ClassifySecondBookTabEntity> {

    private Toolbar toolbar;
    private TextView tv_all;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Button btn_classify_second_toolbar_text;

    private List<String> fragmentList;
    private List<Discover_ClassifySecondBookTabEntity.ListEntity> tabList;
    private Discover_ClassifySecondVPAdapter discover_classifySecondVPAdapter;
    private Retrofit retrofit;
    private String categoryId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover_classify_second_base);

        initData();
        initView();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.classify_second_toolbar);
        tv_all = (TextView) findViewById(R.id.btn_classify_second_toolbar_text);
        tabLayout = (TabLayout) findViewById(R.id.classify_second_tabLayout);
        viewPager = (ViewPager) findViewById(R.id.classify_second_viewpager);
        btn_classify_second_toolbar_text = (Button) findViewById(R.id.btn_classify_second_toolbar_text);

        //绑定tabLayout 和 Viewpager
        tabLayout.setupWithViewPager(viewPager);

        fragmentList = new ArrayList<>();
        tabList = new ArrayList<>();

        for (int i = 0; i < tabList.size(); i++) {
            fragmentList.add(tabList.get(i).getTname());
        }
        discover_classifySecondVPAdapter = new Discover_ClassifySecondVPAdapter(getSupportFragmentManager(), tabList,categoryId);
        viewPager.setAdapter(discover_classifySecondVPAdapter);

    }

    public void initData() {

        retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Intent intent = getIntent();
        String btnTitle = intent.getStringExtra("title");
        categoryId = intent.getStringExtra("id");

        switch (btnTitle){
            case "有声书":
                downloadData(categoryId);
                break;
            case "音乐":
                downloadData(categoryId);
                break;
            case "娱乐":
                downloadData(categoryId);
                break;
            case "相声评书":
                downloadData(categoryId);
                break;
            case "儿童":
                downloadData(categoryId);
                break;
            case "3D体验馆":
                downloadData(categoryId);
                break;
            case "资讯":
                downloadData(categoryId);
                break;
            case "脱口秀":
                downloadData(categoryId);
                break;
            case "情感生活":
                downloadData(categoryId);
                break;
            case "历史":
                downloadData(categoryId);
                break;
            case "人文":
                downloadData(categoryId);
                break;
            case "教育培训":
                downloadData(categoryId);
                break;
            case "英语":
                downloadData(categoryId);
                break;
            case "小语种":
                downloadData(categoryId);
                break;
            case "百家讲坛":
                downloadData(categoryId);
                break;
            case "广播剧":
                downloadData(categoryId);
                break;
            case "戏曲":
                downloadData(categoryId);
                break;
            case "电台":
                downloadData(categoryId);
                break;
            case "商业财经":
                downloadData(categoryId);
                break;
            case "IT科技":
                downloadData(categoryId);
                break;
            case "健康养生":
                downloadData(categoryId);
                break;
            case "旅游":
                downloadData(categoryId);
                break;
            case "汽车":
                downloadData(categoryId);
                break;
            case "动漫游戏":
                downloadData(categoryId);
                break;
            case "电影":
                downloadData(categoryId);
                break;
            case "名校公开":
                downloadData(categoryId);
                break;
            case "时尚生活":
                downloadData(categoryId);
                break;
            case "诗歌":
                downloadData(categoryId);
                break;

        }

    }

    private void downloadData(String categoryId) {

        Discover_ClassifySecondGetTabService discover_classifySecondBookTabService = retrofit.create(Discover_ClassifySecondGetTabService.class);
        discover_classifySecondBookTabService.getBookTap(categoryId).enqueue(this);
    }


    public void finishClassifySecondActivityBtn(View view) {
        finish();
    }

    public void classifySecondDropDownMenuBtn(View view) {

    }

    public void classifySecondSearchBtn(View view) {

    }

    @Override
    public void onResponse(Call<Discover_ClassifySecondBookTabEntity> call, Response<Discover_ClassifySecondBookTabEntity> response) {
        Discover_ClassifySecondBookTabEntity body = response.body();
        String tabTitle = body.getCategoryInfo().getTitle();
        btn_classify_second_toolbar_text.setText(tabTitle);
        tabList.addAll(body.getList());
        discover_classifySecondVPAdapter.notifyDataSetChanged();

    }

    @Override
    public void onFailure(Call<Discover_ClassifySecondBookTabEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
